package com.shundr.truck.http;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.shundr.auth.frame.constant.LCommands;
import com.shundr.auth.frame.http.HttpResult;
import com.shundr.auth.frame.http.LHttpRequest;
import com.shundr.auth.frame.http.LHttpRequest.RequestCompleteListener;
import com.shundr.truck.model.TruckInfo;

/**
 * 车辆Request
 * 
 * @author Administrator
 *
 */
public class UserTruckRequest extends LHttpRequest implements
		RequestCompleteListener {
	public static final int GET_TRUCK_LIST_SUCCESSED = 1;// 获取车辆列表成功
	public static final int GET_TRUCK_INFO_SUCCESSED = 2;// 获取车辆详情成功
	public static final int ADD_TRUCK_SUCCESSED = 3;// 增加车辆成功
	public static final int DELETE_TRUCK_SUCCESSED = 4;// 删除车辆成功
	public static final int UPDATE_TRUCK_SUCCESSED = 5;// 修改车辆信息成功
	public static final int ADD_USER_TRUCK_RETURN_SUCCESSED = 6;// 添加用户车辆返回成功

	private Handler mHandler;
	private Message mMessage;
	private Context mContext;

	private Map<String, Object> param;
	private String command;

	public UserTruckRequest(Context context, Handler handler) {
		this.mContext = context;
		this.mHandler = handler;
		this.mMessage = mHandler.obtainMessage();
		this.setRequestCompleteListener(this);
	}

	/**
	 * getTruckList
	 * 
	 * @param truckStatus
	 * @param offset
	 * @param pageSize
	 */
	public void getTruckList(int truckStatus, int offset, int pageSize) {
		param = new HashMap<String, Object>();
		param.put("truckStatus", truckStatus);
		param.put("offset", offset);
		param.put("pageSize", pageSize);
		command = LCommands.GET_TRUCK_LIST;
		requestByPost(param);
	}

	/**
	 * getTruckInfo
	 * 
	 * @param truckId
	 */
	public void getTruckInfo(int truckId) {
		param = new HashMap<String, Object>();
		param.put("truckId", truckId);
		command = LCommands.GET_TRUCK_INFO;
		requestByPost(param);
	}

	/**
	 * 
	 * addTruck(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param truckTypeCode
	 * @param expectAddrTxtAll
	 * @param truckLoad
	 * @param truckLength
	 * @param truckidNum
	 * @param truckstatus
	 * @param expectAddrCodeAll
	 * @param base64
	 * @exception
	 * @since 1.0.0
	 */
	public void addTruck(String truckidNum, String truckTypeCode,
			String truckLoad, String truckLength, String expectAddrTxtAll,
			String expectAddrCodeAll, int truckstatus, String base64) {
		param = new HashMap<String, Object>();
		param.put("truckidNum", truckidNum);
		param.put("truckTypeCode", truckTypeCode);
		param.put("truckLoad", truckLoad);
		param.put("truckLength", truckLength);
		param.put("expectAddrTxtAll", expectAddrTxtAll);
		param.put("expectAddrCodeAll", expectAddrCodeAll);
		param.put("truckstatus", truckstatus);
		param.put("fileObjectListJson", base64);
		command = LCommands.ADD_TRUCK;
		requestByPost(param);
	}

	/**
	 * deleteTruck
	 * 
	 * @param truckId
	 */
	public void deleteTruck(int truckId) {
		param = new HashMap<String, Object>();
		param.put("truckId", truckId);
		command = LCommands.DELETE_MYTRUCK_BYID;
		requestByPost(param);
	}

	/**
	 * updateTruck
	 * 
	 * @param truckTypeCode
	 * @param truckid
	 * @param expectAddrTxtAll
	 * @param truckLoad
	 * @param truckLength
	 * @param truckidNum
	 * @param expectAddrCodeAll
	 * @param truckpicpath
	 * @param truckLicensePicPath
	 */
	public void updateTruck(int truckTypeCode, int truckid,
			String expectAddrTxtAll, Float truckLoad, Float truckLength,
			String truckidNum, String expectAddrCodeAll, String truckpicpath,
			String truckLicensePicPath) {
		param.put("truckTypeCode", truckTypeCode);
		param.put("truckid", truckid);
		param.put("expectAddrTxtAll", expectAddrTxtAll);
		param.put("truckLoad", truckLoad);
		param.put("truckLength", truckLength);
		param.put("truckidNum", truckidNum);
		param.put("expectAddrCodeAll", expectAddrCodeAll);
		param.put("truckPicPath", truckpicpath);
		param.put("truckLicensePicPath", truckLicensePicPath);
		command = LCommands.UPDATE_TRUCK;
		requestByPost(param);
	}

	public void addUserAndTruck(String phone, String truckIDNum,
			String truckLoad, String truckLength, int typeCode,
			String expectedAddr, String expectedAddrCode, String base64) {
		param = new HashMap<String, Object>();
		param.put("phone", phone);
		param.put("truckidNum", truckIDNum);
		param.put("truckTypeCode", typeCode);
		param.put("truckLoad", truckLoad);
		param.put("truckLength", truckLength);
		param.put("expectAddrTxtAll", expectedAddr);
		param.put("expectAddrCodeAll", expectedAddrCode);
		param.put("fileObjectListJson", base64);
		command = LCommands.REGIST_USER_AND_TRUCK;
		requestByPost(param);
	}

	@Override
	protected void initParams() {
		super.initParams();
		mParam = param;
		mCommand = command;
		mBaseContext = mContext;
	}

	@Override
	public void requestSuccessed(HttpResult result) {
		if (mHandler == null)
			return;
		if (command.equals(LCommands.GET_TRUCK_LIST)) {
			mMessage.what = GET_TRUCK_LIST_SUCCESSED;
			List<TruckInfo> infos = UserTruckParser.parserTruckList(result
					.getResult());
			mMessage.obj = infos;
		} else if (command.equals(LCommands.ADD_TRUCK)) {
			mMessage.what = ADD_TRUCK_SUCCESSED;
			mMessage.obj = result.getDescription();
		} else if (command.equals(LCommands.DELETE_MYTRUCK_BYID)) {
			mMessage.what = DELETE_TRUCK_SUCCESSED;
			mMessage.obj = result.getDescription();
		} else if (command.equals(LCommands.UPDATE_TRUCK)) {
			mMessage.what = UPDATE_TRUCK_SUCCESSED;
			mMessage.obj = result.getDescription();
		} else if (command.equals(LCommands.GET_TRUCK_INFO)) {
			mMessage.what = GET_TRUCK_INFO_SUCCESSED;
			TruckInfo infos = UserTruckParser.parserTruckInfo(result
					.getResult());
			mMessage.obj = infos;
		} else if (command.equals(LCommands.REGIST_USER_AND_TRUCK)) {
			mMessage.what = ADD_USER_TRUCK_RETURN_SUCCESSED;
		}
		mHandler.sendMessage(mMessage);
	}

	@Override
	public void requestFailed(HttpResult result) {
		if (mHandler == null)
			return;
		// 请求失败,result的返回码可判断失败原因，进行如下操作
		mMessage.what = REQUEST_FAILED;
		mMessage.obj = result.getDescription();
		mHandler.sendMessage(mMessage);
	}

}
